﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Reflection;
using ScrumTable.Common;
using ScrumTable.Common.Threading;
using ScrumTable.DL.Data.Generic.Cache;

#endregion

namespace ScrumTable.DL.Data.Local.Services
{
    /// <summary>
    /// This class is used to initiate the connection and load all needed
    /// information to access data on the project level.
    /// </summary>
    internal class ConnectionCacheLoader
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly DataContext _context;
        private readonly AsyncCallback _callback;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new ConnectionCacheLoader instance, no public constructor
        /// needed.
        /// </summary>
        internal ConnectionCacheLoader(DataContext context, AsyncCallback callback)
        {
            PreCondition.AssertNotNull(context, "context");
            PreCondition.AssertNotNull(callback, "callback");

            _context = context;
            _callback = callback;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Loads the data out from the xml file.
        /// </summary>
        public void LoadData()
        {
            CacheContext cacheContext;
            Exception error = null;

            try
            {
                cacheContext = new CacheContext(_context.Schema);
                _context.DataProvider = new DataProvider(
                    _context.ConfigurationInternal,
                    cacheContext,
                    _context.Connection.ConnectionManager);
                _context.DataProvider.LoadProjects(_context.Connection.ConnectionMoniker);
                cacheContext.Import(_context.DataProvider.Projects);
            }
            catch (Exception e)
            {
                cacheContext = null;
                error = e;
            }
            _callback(new DataResult<CacheContext>(cacheContext, error));
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}